#include <QFileDialog>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>

#include <QDomDocument>
#include "pozicijas.h"

pozicijas::pozicijas()
{
//return QList(
}


pozicijas::~pozicijas()
{
//return QList(
}



void pozicijas::savetofile()
{


QString fileName = QFileDialog::getSaveFileName(0, "Save File",
                        "~/untitled.xml",
                        "xml files (*.xml)");
if(!fileName.isEmpty())
{
//QFile file(fileName);
//QXmlStreamWriter mystream = QXmlStreamWriter( new QFile(fileName)  ) ;
QFile * myfile = new QFile(fileName);
if(myfile->open(QIODevice::WriteOnly))
{
 QXmlStreamWriter stream(myfile  );
stream.setAutoFormatting(true);
 stream.writeStartDocument();

 stream.writeStartElement("pozicijas");
// stream.writeAttribute(QString("nosaukums"),value(i).nosaukums);
 for(int i =0;i<count();i++)
{

 //stream.writeTextElement("title", "Qt Software");
 stream.writeStartElement("pozicija");
  stream.writeAttribute(QString("nosaukums"),value(i).nosaukums);
  stream.writeAttribute(QString("gajsk"),QString("%1").arg(value(i).gajsk).toAscii());
if(value(i).gajsk >0)
  {
  stream.writeStartElement("gajieni");
QString  gajstr = QString("%1").arg(value(i).gajieni[0]);
for(int j=1;j<value(i).gajsk;j++)gajstr += " " + QString("%1").arg(value(i).gajieni[j]);
stream.writeCharacters(gajstr);
stream.writeEndElement(); // gajieni
}
  stream.writeStartElement("stavoklis");
QString  gajstr = QString("%1").arg(value(i).pozdata[0]);
for(int j=1;j<24;j++)gajstr += " " + QString("%1").arg(value(i).pozdata[j]);
stream.writeCharacters(gajstr);
stream.writeEndElement(); // stavoklis
stream.writeEndElement(); // pozicija
 }
 stream.writeEndElement(); // pozicijas

 stream.writeEndDocument();
myfile->close();
}
delete myfile;
}
}


void pozicijas::loadfromfile()
{
QDomDocument doc("mydocument");
QString fileName = QFileDialog::getOpenFileName(0, QString("Open File"),
                                             QString("~"),
                                             QString("xml files (*.xml)"));
if(!fileName.isEmpty())
{
QFile * file  = new QFile(fileName);
if (file->open(QIODevice::ReadOnly))
{
QXmlStreamReader  stream(file);
int ttype = stream.readNext();
while(  ttype  != QXmlStreamReader::Invalid &&   !stream.atEnd())
{
 if(ttype == QXmlStreamReader::StartElement)
{

if(stream.name().toString() == "pozicija")
{
append(pozicija());
last().nosaukums = stream.attributes().value("nosaukums").toString();
last().gajsk = stream.attributes().value("gajsk").toString().toInt();
}

if(stream.name().toString() == "gajieni")
{
    QStringList gajlst = stream.readElementText().split(" ");
for(int i=0;i<last().gajsk && i< gajlst.count() && i<14 ;i++)
 last().gajieni[i] = gajlst[i].toInt();


}


if(stream.name().toString() == "stavoklis")
{
    QStringList gajlst = stream.readElementText().split(" ");
for(int i=0;i<gajlst.count() && i<24 ;i++)
 last().pozdata[i] = gajlst[i].toInt();


}



//QMessageBox::information ( 0, QString("info"),stream.name().toString());
}
  ttype = stream.readNext();

}

file->close();
}
delete file;

}
}
